<?php
	use\ampleharvest\code\usermanager\tables;
	/**
	 * @package AmpleHarvest
	 */

	require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/config/cConfig.php';
	$codeConfig = new \ampleharvest\code\cConfig();
	
	require_once dirname(dirname(__FILE__)) . "/cMailNotification.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/User_Manager/Tables/cTABLE_Groups.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/Tables/cTABLE_Harvest_Requests.php";
	require_once $codeConfig->C_STR_USER_MANAGER_PATH . "/Classes/cTABLE_Users.php";
	require_once $codeConfig->C_STR_USER_MANAGER_PATH . "/Classes/cTABLE_Users2Groups.php";

	/**
	 * @brief Class Extends the cMailNotification Class and is used to send notifictions about Queries
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 13 APR 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes
	 */
	class cRequestNotification extends cMailNotification
	{
		const C_INT_TYPE_NEW = 0;
		const C_INT_TYPE_UPDATE = 1;

		protected $m_int_Type = SELF::C_INT_TYPE_NEW;

		protected $m_str_RequestID = null;

		protected $m_obj_TABLE_Users = null;
		protected $m_obj_TABLE_Users2Groups = null;
		protected $m_obj_TABLE_Harvest_Request = null;
		protected $m_obj_TABLE_Orig_Harvest_Request = null;

		public function __construct()
		{
			$this->m_obj_TABLE_Users = new \cTABLE_Users();
			$this->m_obj_TABLE_Users2Groups = new \cTABLE_Users2Groups();
			$this->m_obj_TABLE_Harvest_Request = new cTABLE_Harvest_Requests();
			$this->m_obj_TABLE_Orig_Harvest_Request = new cTABLE_Harvest_Requests();
		}

		/**
		 * Sets the original data (used when sending and update Notification.
		 * @param cTABLE_Harvest_Requests $obj_OrigData The original data object
		 */
		public function set_OriginalData(cTABLE_Harvest_Requests $obj_OrigData)
		{
			$this->m_obj_TABLE_Orig_Harvest_Request = $obj_OrigData;
		}

		/**
		 * Sets the type of notification that will be sent
		 * @param integer $int_Type see constants
		 */
		public function set_Type($int_Type)
		{
			$this->m_int_Type = $int_Type;
		}

		/**
		 * Sets the ID of the harvest Request
		 * @param string $str_RequestID ID of the Request the notification is about (GUID)
		 */
		public function set_RequestID($str_RequestID)
		{
			$this->m_str_RequestID = $str_RequestID;
		}

		public function send($str_UserName = null)
		{
			if (!$this->m_obj_TABLE_Harvest_Request->load_DataFromID($this->m_str_RequestID))
			{
				return false;
			}

			foreach ($this->m_obj_TABLE_Users2Groups->get_MembersAsArray(tables\cTABLE_Groups::C_STR_GROUP_HARVEST_MANAGER) as $str_UserID)
			{
				if ($this->m_obj_TABLE_Users->load_DataFromUserID($str_UserID))
				{
					$this->add_To($this->m_obj_TABLE_Users->get_Email1());
				}
			}

			$this->m_obj_TABLE_Users->load_DataFromUserID($this->m_obj_TABLE_Harvest_Request->get_user_id());

			$this->add_CC($this->m_obj_TABLE_Users->get_Email1());

			if ($str_UserName)
			{
				$this->m_obj_TABLE_Users->load_DataFromUserName($str_UserName);
				$this->add_CC($this->m_obj_TABLE_Users->get_Email1());
			}

			switch ($this->m_int_Type)
			{
				case self::C_INT_TYPE_NEW:
					$this->set_Subject("New Harvest Request");
					break;
				case self::C_INT_TYPE_UPDATE:
					$this->set_Subject("Harvest Request " . $this->m_str_RequestID . " has been updated");
					break;
			}

			$int_Errors = ini_get("display_errors");
			ini_set('display_errors', 0);
			$bool_Return = parent::send();

			ini_set('display_errors', $int_Errors);

			return $bool_Return;
		}

		public function get_Body()
		{
			$str_Output = "";

			switch ($this->m_int_Type)
			{
				case self::C_INT_TYPE_NEW:
					$str_Output .= "The following Request has been Received :<hr/>\n";
					$str_Output .= $this->output_Record($this->m_obj_TABLE_Harvest_Request);
					break;
				case self::C_INT_TYPE_UPDATE:
					$str_Output .= "The following Request has been updated from :<hr/>\n";
					$str_Output .= $this->output_Record($this->m_obj_TABLE_Orig_Harvest_Request);
					$str_Output .= "to:<hr/>\n";
					$str_Output .= $this->output_Record($this->m_obj_TABLE_Harvest_Request);
					break;
			}

			return $str_Output;
		}

		private function output_Record(cTABLE_Harvest_Requests $obj_Data)
		{
			$arr_Data = $obj_Data->output_AsArray();
			$str_Output = "<table><tbody>";

			foreach ($arr_Data as $key => $data)
			{
				$str_Output .= "<tr>";
				switch ($key)
				{
					case $this->m_obj_TABLE_Harvest_Request->get_Column_Name($this->m_obj_TABLE_Harvest_Request->m_int_COL_UserID):
						$this->m_obj_TABLE_Users->load_DataFromUserID($data);
						$str_Output .= "<td><b>User Name</b></td><td>" . $this->m_obj_TABLE_Users->get_UserName() . "</td>\n";
						break;
					case $this->m_obj_TABLE_Harvest_Request->get_Column_Name($this->m_obj_TABLE_Harvest_Request->m_int_COL_Status):
						$this->m_obj_TABLE_Users->load_DataFromUserID($data);
						$str_Output .= "<td><b>$key</b></td><td>" . $this->m_obj_TABLE_Harvest_Request->convert_StatusValue2Label($data) . "</td>\n";
						break;
					default:
						$str_Output .= "<td><b>$key</b></td><td> $data</td>\n";
				}
				$str_Output .= "</tr>";
			}
			$str_Output .= "</tbody></table>";
			return $str_Output;
		}
	}
?>